package cn.wqb.bpp.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    /**
     * 按时间范围搜索时，endDate必须加一天，否则搜索不到endDate当天的信息
     * @param date
     * @return
     */
    public static String addOneDay(String date) {
        if (date != null && date.length() > 0) {
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date d = format.parse(date);
                Calendar c = Calendar.getInstance(Locale.CHINA);
                c.setTime(d);
                c.add(Calendar.DAY_OF_MONTH, 1);
                d = c.getTime();
                return format.format(d);
            } catch (ParseException e) {
                LogUtils.instance(DateUtils.class).error(e + ">>>>>>>>>>Cause:" + e.getCause());
            }
        }
        return date;
    }

    /**
     * 按月份获取该月的天数
     */
    public static int getMonthDays(int year, int month) {
        switch (month){
            case 2:
                if(year%4==0){
                    return 29;
                }
                return 28;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
        }
        return 30;
    }

    /**
     * 获取每月月底的日期，一位数补0
     *
     * @param month
     * @return
     */
    public static String getEndDayOfMonth(String month) {
        String[] arr = month.split("\\-");
        if (arr[1].startsWith("0")) {
            arr[1] = arr[1].substring(1);
        }
        int days = getMonthDays(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]));
        if (days < 10) {
            return "0" + days;
        }
        return days + "";
    }

    /**
     * 获取24小时以前的时间
     * @return
     */
    public static String get24HoursAgo() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currentTime = System.currentTimeMillis() - 24 * 60 * 60 * 1000;//24小时以前的时间
        Date date = new Date(currentTime);
        return format.format(date);
    }

    /**
     * 获取当前时间戳
     * @return
     */
    public static long getCurrTimestamp() {
        return System.currentTimeMillis();
    }

    /**
     * 组装从本月1号到今天的日期范围参数
     * @return
     */
    public static Map<String, Object> getThisMonthParams() {
        Map<String, Object> params = new HashMap<>();
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int date = cal.get(Calendar.DATE);
        String dateStr = date + "";
        if (date < 10) {
            dateStr = "0" + date;
        }
        params.put("beginDate", year + "-" + month + "-01");
        params.put("endDate", DateUtils.addOneDay(year + "-" + month + "-" + dateStr));
        return params;
    }

    /**
     * 组装今天的日期参数
     * @return
     */
    public static Map<String, Object> getToDayParams() {
        Map<String, Object> params = new HashMap<>();
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int date = cal.get(Calendar.DATE);
        String dateStr = date + "";
        if (date < 10) {
            dateStr = "0" + date;
        }
        params.put("beginDate", year + "-" + month + "-" + dateStr);
        params.put("endDate", DateUtils.addOneDay(year + "-" + month + "-" + dateStr));
        return params;
    }

    /**
     * 把数据库时间yyyy-MM-dd HH:mm:ss转成yyyy-MM-dd形式
     * @param date
     * @return
     */
    public static String dbTimeToDay(String date){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
            return format2.format(format.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

}
